package
{
	import events.ModulEvent;
	
	import flash.display.GradientType;
	import flash.display.InteractiveObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.text.TextField;
	import flash.utils.getDefinitionByName;
	
	import global.GameState;
	
	import utils.ColorTool;
	import utils.TextTool;
	
	import view.components.Input;
	import view.components.LoadingBar;

	
	public class PlayerInfoModul extends Sprite
	{
		private var nameInput:Input;
		private var jifenInput:TextField;
		private var moneyInput:Input;
		private var blueInput:Input;
		private var infoInput:*;
		
		private var ImgButtonClass:*;
		private var ResouceClass:*;
		private var LabelButtonClass:*;
		private var RemoteHtmlImgClass:*;
		private var ControlClass:*;
		
		private var refreshBut:InteractiveObject;
		private var feedBackBut:InteractiveObject;
		private var taskBut:InteractiveObject;
		private var payBut:InteractiveObject;
		private var itemBut:InteractiveObject;
		private var chestBut:InteractiveObject;
		
		private var autoImg:*;
		private var expBar:LoadingBar;
		private var vipImg:InteractiveObject
		
		public function PlayerInfoModul()
		{
			
		}
		
		
		
		public function init():void
		{
			initClass();
			initBg();
			initInputs();
			initStage();
			initButs();
			initImg();
			initExpBar();
			ColorTool.dropShadow(this);
		}
		
		
		
		private function initClass():void
		{
			ImgButtonClass = getDefinitionByName("view.components.ImgButton");
			ResouceClass = getDefinitionByName("global.Resource");
			LabelButtonClass = getDefinitionByName("view.components.LabelButton");
			RemoteHtmlImgClass = getDefinitionByName("view.components.RemoteHtmlImg");
			ControlClass = getDefinitionByName("core.Control");
		}
		
		private function initExpBar():void
		{
			expBar = new LoadingBar("face_expFace","face_expBg");
			expBar.x = 117;
			expBar.y = 38;
			addChild(expBar);
			expBar.percent = .5;
			
			expBar.mouseEnabled = false;
			expBar.addEventListener(MouseEvent.MOUSE_OVER,onBarOver);
			expBar.addEventListener(MouseEvent.MOUSE_OUT,onBarOut);
		}
		
		private function onBarOver(event:MouseEvent):void
		{
			ControlClass.getInstance().hintControl.showHint(expBar,"<font size='12' color='#0'>" + expBar.toString() + "</font>");
		}
		
		private function onBarOut(event:MouseEvent):void
		{
			ControlClass.getInstance().hintControl.hideHint();
		}
		
		private function initImg():void
		{
			addChild(ResouceClass.getImgXY(90,57,"face_gold"));
			addChild(ResouceClass.getImgXY(210,55,"face_bank"));
			//addChild(ResouceClass.getImgXY(15,9,"face_head"));
			
			autoImg = new RemoteHtmlImgClass(50,50);
			autoImg.x = 15;
			autoImg.y = 9;
			addChild(autoImg);
			
			
		}
		
		private function initButs():void
		{
			payBut = new ImgButtonClass(230,7,0,onBut,48,20,"butBg",null,"银行",12,0xFFFFFF,false);
			payBut.mouseEnabled = false;
			addChild(payBut);
			addChild(new ImgButtonClass(300,7,1,onBut,48,20,"butBg",null,"反馈",12,0xFFFFFF,false));
			
			
			
			itemBut = new ImgButtonClass(300,30,2,onBut,48,20,"butBg",null,"道具",12,0xFFFFFF,false);
			itemBut.mouseEnabled = false;
			addChild(itemBut);

			
			taskBut = new ImgButtonClass(230,30,3,onBut,48,20,"butBg",null,"任务",12,0xFFFFFF,false);
			taskBut.mouseEnabled = false;
			addChild(taskBut);
			
			refreshBut = new LabelButtonClass(320,57,4,onBut,"panel_refresh");
			(refreshBut as Object).info = "刷新";
			addChild(refreshBut);
			refreshBut.mouseEnabled = false;
			
			chestBut = new ImgButtonClass(300,83,5,onBut,48,20,"butBg3",null,"领 奖",12,0xFFFFFF);
			(chestBut as Object).isLight = false;
			chestBut.mouseEnabled = false;
			addChild(chestBut);
		}
		
		private function onBut(event:MouseEvent):void
		{
			switch(event.target.id)
			{
				case 0://银行
					dispatchEvent(new ModulEvent(ModulEvent.BANK));
					break;
				case 1://反馈
					dispatchEvent(new ModulEvent(ModulEvent.FEED_BACK));
					break;
				case 2://道具
					dispatchEvent(new ModulEvent(ModulEvent.ITEM));
					break;
				case 3://任务
					dispatchEvent(new ModulEvent(ModulEvent.TASK));
					break;
				case 4://更新
					refreshBut.mouseEnabled = false;
					dispatchEvent(new ModulEvent(ModulEvent.RE_FRESH));
					break;
				case 5://领奖
					//chestBut.mouseEnabled = false;
					dispatchEvent(new ModulEvent(ModulEvent.REWARD));
					break;
				case 6:
					dispatchEvent(new ModulEvent(ModulEvent.VIPSET));
					break;
			}
		}
		
		public function disableReward():void
		{
			//chestBut.mouseEnabled = false;
			refreshBut.mouseEnabled = false;
			dispatchEvent(new ModulEvent(ModulEvent.RE_FRESH));
		}
		
		private function initStage():void
		{
			//stage.scaleMode = StageScaleMode.NO_SCALE;
		}
		
		private function initBg():void
		{
			var fillType:String = GradientType.LINEAR; 
			//var colors:Array = [0xB7B7B7, 0x333333];
			var colors:Array = [0xFFE200, 0xA84400]
			var alphas:Array = [1, 1];
			var ratios:Array = [0x00, 0xFF];
			var matr:Matrix = new Matrix();
			matr.createGradientBox(364, 157, Math.PI / 2, 0, 0);
			
			with (this.graphics)
			{
				clear();
				beginGradientFill(fillType,colors,alphas,ratios,matr);
				drawRoundRect(0,0,364,157,15,15);
				endFill();
				colors = [0xE07C00,0x7D1900];
				matr.createGradientBox(364 - 6,157 - 6,Math.PI / 2,0,0);
				beginGradientFill(fillType,colors,alphas,ratios,matr);
				drawRoundRect(3,3,364 - 6, 157 - 6,15,15);
				endFill();
			}
		}
		
		private function initInputs():void
		{
			nameInput = new Input(106,19);
			nameInput.x = 90;
			nameInput.y = 9;
			addChild(nameInput);
			
			jifenInput = TextTool.getTextField(40,20,12,0xFFFFFF);
			jifenInput.x = 90;
			jifenInput.y = 32;
			addChild(jifenInput);
			
			moneyInput = new Input(85,19,0xFFFF00,true,"right");
			moneyInput.x = 110;
			moneyInput.y = 57;
			addChild(moneyInput);
			
			blueInput = new Input(85,19,0xFFFFFF,true,"right");
			blueInput.x = 230;
			blueInput.y = 57;
			addChild(blueInput);
			
			infoInput = TextTool.getTextArea(334,70);
			infoInput.alpha = .5;
			infoInput.x = 15;
			infoInput.y = 80;
			addChild(infoInput);
			
			nameInput.mouseChildren = false;
			//jifenInput.mouseChildren = false;
			moneyInput.mouseChildren = false;
			blueInput.mouseChildren = false;
			infoInput.mouseChildren = false;
		}
		
		public function update(xml:XML):void
		{
			nameInput.text = xml.info.@name.toString();
			jifenInput.text = xml.info.@level.toString() + "级";
			moneyInput.text = xml.info.@gameMoney.toString();
			blueInput.text = xml.info.@cloudMoney.toString();
			
			var exp:int = int(xml.info.@exp);
			var expMax:int = int(xml.info.@maxExp);
			
			//expBar.percent = exp / expMax;
			expBar.setProgress(exp,expMax);
			
			autoImg.loadImg(xml.info.@headImg.toString());
			
			
			if (xml.info.@isDayBonuse.toString() == "1")
			{
				chestBut.mouseEnabled = false;	
			}
			else
			{
				chestBut.mouseEnabled = true;
			}
			
			refreshBut.mouseEnabled = true;
			expBar.mouseEnabled = true;
			itemBut.mouseEnabled = true;
			payBut.mouseEnabled = true;
			taskBut.mouseEnabled = true;
			
			GameState.isShowVip = Boolean(int(xml.info.@showVip));
			GameState.vipTime = String(xml.info.@vipDeadTime);
			
			var vip:int = int(xml.info.@vip);
			
			
			if (vip == GameState.vipType)
			{
				return;
			}
			
			if (vipImg)
			{
				if (vipImg.parent == this);
				{
					this.removeChild(vipImg);
					(vipImg as LabelButtonClass).destroy();
				}
			}
			
			switch(vip)
			{
				case 1:
				case 2:
				case 3:
				case 4:
					vipImg = new LabelButtonClass(164,8,6,onBut,"face_vip" + vip);
					(vipImg as Object).info = "VIP设置";
					addChild(vipImg);
					break;
				
			}
			
			
		}
		
		public function updateServerMsg(xml:XML):void
		{
			var str:String = "";
			for each (var xml:XML in xml.children())
			{
				str += xml.@content.toString() + "\n";
			}
			infoInput.text = str;
		}
	}
}